﻿using System;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Xml;

public static class Enum<T>
{
    public static T Parse(string value)
    {
        return Enum<T>.Parse(value, true);
    }

    public static T Parse(string value, bool ignoreCase)
    {
        return (T)Enum.Parse(typeof(T), value, ignoreCase);
    }

    public static bool TryParse(string value, out T returnedValue)
    {
        return Enum<T>.TryParse(value, true, out returnedValue);
    }

    public static bool TryParse(string value, bool ignoreCase, out T returnedValue)
    {
        try
        {
            returnedValue = (T)Enum.Parse(typeof(T), value, ignoreCase);
            return true;
        }
        catch
        {
            returnedValue = default(T);
            return false;
        }
    }
}

public static class FieldTypeExtension
{
    public static Type SystemTypeOrString(this FieldType fieldType)
    {
        if (fieldType == FieldType.Boolean)
            return typeof(bool);
        else if (fieldType == FieldType.DateTime)
            return typeof(DateTime);
        else if (fieldType == FieldType.Double)
            return typeof(double);
        else if (fieldType == FieldType.Guid)
            return typeof(Guid);
        else if (fieldType == FieldType.History)
            return typeof(string);
        else if (fieldType == FieldType.Html)
            return typeof(string);
        else if (fieldType == FieldType.Integer)
            return typeof(int);
        else if (fieldType == FieldType.PlainText)
            return typeof(string);
        else if (fieldType == FieldType.String)
            return typeof(string);
        else if (fieldType == FieldType.TreePath)
            return typeof(string);
        else
            return typeof(string);
    }
}

public static class XmlConvert2
{
    public static object ToObject(string value, FieldType fieldType)
    {
        if (fieldType == FieldType.Boolean)
            return XmlConvert.ToBoolean(value);
        else if (fieldType == FieldType.DateTime)
            return XmlConvert.ToDateTime(value);
        else if (fieldType == FieldType.Double)
            return XmlConvert.ToDouble(value);
        else if (fieldType == FieldType.Guid)
            return XmlConvert.ToGuid(value);
        else if (fieldType == FieldType.History)
            throw new NotSupportedException();
        else if (fieldType == FieldType.Html)
            return value;
        else if (fieldType == FieldType.Integer)
            return XmlConvert.ToInt32(value);
        else if (fieldType == FieldType.PlainText)
            return value;
        else if (fieldType == FieldType.String)
            return value;
        else if (fieldType == FieldType.TreePath)
            throw new NotSupportedException();
        else
            throw new NotImplementedException();
    }
}